const fs = require("fs");
const { extname } = require("path");
const mine = require("./mine");
const ejs=require("ejs");
const app = {
    // 静态服务器
    static: (req, res, staticPath) => {
        let path = new URL(req.url, "http://localhost");
        // 默认index
        pathname = path.pathname == '/' ? '/index.html' : path.pathname
        try {
            let data = fs.readFileSync(staticPath + pathname)
            res.writeHead(200, { 'Content-Type': mine.getMine(extname(req.url)) + ";charset=UTF8" })
            res.end(data)
        } catch (err) {
            // 向下动态路由执行
            // res.writeHead(404, { 'Content-Type': "text/html;charset=UTF8" })
            // res.end("页面未找到")
        }
    },
    // 动态路由
    login:(req,res)=>{//登录页面
        ejs.renderFile('./static/login.html',{msg:"ok"},(err,data)=>{
            res.writeHead(200, { 'Content-Type': "text/html;charset=UTF8" })
            res.end(data)
        })
    },

    doLogin: (req, res) => {
        let postData = "";
        req.on("data", (chunk) => {
            postData += chunk;
        })
        req.on("end", () => {
            res.writeHead(200, { 'Content-Type': "text/html;charset=UTF8" })
            res.end(postData)
        })
    },
    error:(req, res)=>{
        res.end("error")
    }
}
module.exports = app